package com.sfh.cp.msg.pst;


import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import crm.sfh.server.MangeNote;
import crm.sfh.server.SNote;
import crm.sfh.server.SNotice;
import crm.sfh.server.entity.EContent;
import crm.sfh.server.entity.ENotice;
import crm.sfh.server.entity.req.ReqPage;
import crm.sfh.server.entity.rsp.TPage;

public class PstMessage extends AbstractPresenter<IPstMessage.IVMessage> implements IPstMessage {

    @Override
    public void listPage(final  ReqPage req) {
        BmobQuery<SNotice> query = new BmobQuery<>();

        // 根据score字段降序显示数据
        query.order("-createdAt");
        query.setSkip((req.nowPage - 1) * 10);
        query.setLimit(req.nowPage * 10);
        query.findObjects(AppCacheManager.getApplication(), new FindListener<SNotice>() {
            @Override
            public void onSuccess(List<SNotice> list) {

                TPage<ENotice> page = new TPage<>();
                page.setNowPage(req.nowPage);
                page.setTotalPage(list.size() >= 10 ? req.nowPage + 1 : req.nowPage);
                List<ENotice> contents = new ArrayList<>(10);

                for (SNotice note : list) {
                    contents.add(note.getENotice());
                }
                page.setList(contents);
                getView().onSuccess(page);
            }

            @Override
            public void onError(int i, String s) {
                getView().onError("未查询到数据");
            }
        });
    }
}